<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    center
    width="600px">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="区域名称" prop="regionName">
      <el-input v-model="dataForm.regionName" placeholder="区域名称"></el-input>
    </el-form-item>
    <el-form-item label="餐台个数" prop="seatNum">
      <el-input-number :min="1" :max="100" v-model="dataForm.seatNum" placeholder="餐台个数"></el-input-number>
    </el-form-item>
	<el-form-item label="类型" prop="storeTypeId">
	<el-select v-model="dataForm.storeTypeId">
		<el-option
		 v-for="item in storeTypeList"
		 :label="item.storeTypeName"
		 :key="item.id"
		 :value="item.id">
		</el-option>
	</el-select>
	</el-form-item>
    <el-form-item label="最低消费">
		  <el-input v-model="dataForm.minConsume" placeholder="最低消费" @input="checkNumber"></el-input>
    </el-form-item>
    <el-form-item label="备注">
      <el-input v-model="dataForm.remark" type="textarea" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="disabledSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
		storeTypeList:[],
		disabledSubmit:false,
        visible: false,
        dataForm: {
          id: 0,
          regionNo: '',
          regionName: '',
          sellerId: '',
          seatNum: '',
          minConsume: '',
          status: '',
          remark: '',
		  storeTypeId:''
        },
        dataRule: {
          regionNo: [
            { required: true, message: '区域编号不能为空', trigger: 'blur' }
          ],
          regionName: [
            { required: true, message: '区域名称不能为空', trigger: 'blur' }
          ],
          storeTypeId: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
          sellerId: [
            { required: true, message: '所属租户不能为空', trigger: 'blur' }
          ],
          seatNum: [
            { required: true, message: '座位数不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '区域状态(1正常2撤销3暂时不能用)不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      checkNumber(){
        if (isNaN(this.dataForm.minConsume)){
          this.dataForm.minConsume = ''
        }
      },

      init (id) {

		//餐厅类型
		this.$http({
		  url: this.$http.adornUrl('/store/tstoretype/list'),
		  method: 'get',
		}).then(({data}) => {
		  if (data && data.code === 0) {
		    this.storeTypeList = data.page.list
		  } else {
		    this.storeTypeList = []
		  }
		})

        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstoreregion/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.regionNo = data.tStoreRegion.regionNo
                this.dataForm.regionName = data.tStoreRegion.regionName
                this.dataForm.sellerId = data.tStoreRegion.sellerId
                this.dataForm.seatNum = data.tStoreRegion.seatNum
                this.dataForm.minConsume = data.tStoreRegion.minConsume
                this.dataForm.status = data.tStoreRegion.status
                this.dataForm.remark = data.tStoreRegion.remark
				this.dataForm.storeTypeId = data.tStoreRegion.storeTypeId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
				this.disabledSubmit = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstoreregion/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'regionNo': this.dataForm.regionNo,
                'regionName': this.dataForm.regionName,
                'sellerId': this.dataForm.sellerId,
                'seatNum': this.dataForm.seatNum,
                'minConsume': this.dataForm.minConsume,
                'status': this.dataForm.status,
                'remark': this.dataForm.remark,
				        'storeTypeId':this.dataForm.storeTypeId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
					          this.disabledSubmit = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.disabledSubmit = false
                this.$message.error(data.msg)
              }
            })
          }else {
            this.disabledSubmit = false
          }
        })
      }
    }
  }
</script>
